using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using _Interface;

namespace script_tester
{
    class plugin
    {
        public bool Error = false;
        private string filename = null;
        private CSharpCodeProvider prov = null;
        public _Interface.IPlugin _ip_plugin = null;
        public CompilerErrorCollection _error = null;

        public plugin(string filename) 
        {
            
            this.filename = filename;
            prov = new CSharpCodeProvider();
            ICodeCompiler compiler = prov.CreateCompiler();
            CompilerParameters cp = new CompilerParameters();
            cp.GenerateExecutable = false;
            cp.GenerateInMemory = true;
            cp.ReferencedAssemblies.Add("System.dll");
            cp.ReferencedAssemblies.Add("interface.dll");

            CompilerResults _cr_result = compiler.CompileAssemblyFromFile(cp, this.filename);
            if (_cr_result.Errors.Count == 0)
            {
                System.Reflection.Assembly _assembly = _cr_result.CompiledAssembly;
                Type[] _t_ClassType = _assembly.GetTypes();
                Type _t_plug = _t_ClassType[0].GetInterface("_Interface.IPlugin",true);
                _ip_plugin = (_Interface.IPlugin)_assembly.CreateInstance(_t_ClassType[0].ToString());
                
            
                
            }
            else
            {
                this.Error = true;
                _error = _cr_result.Errors;
                foreach(CompilerError _cp_error in _cr_result.Errors )
                {
                Console.WriteLine("Error: #"+_cp_error.ErrorNumber+" Line: "+_cp_error.Line.ToString()+"  Message: "+_cp_error.ErrorText);
                }
            }
            
        }

    
    }
}
